package com.tarena.demo.luban.all.main.service.impl;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.Sph;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.demo.luban.all.main.mapper.OrderMapper;
import com.tarena.demo.luban.all.main.rpc.CartDeleteRpc;
import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.cart.api.CartApi;
import com.tarena.demo.luban.protocol.cart.param.CartDeleteParam;
import com.tarena.demo.luban.protocol.order.dos.OrderDO;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import com.tarena.demo.luban.protocol.stock.param.StockReduceCountParam;
import com.tarena.luban.demo.stock.api.StockApi;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Service
public class OrderServiceImpl implements OrderService {
    /*@Autowired
    private CartService cartService;
    @Autowired
    private StockService stockService;*/
    @Autowired
    private CartApi cartApi;
    @Autowired
    private StockApi stockApi;
    @Autowired
    private OrderMapper orderMapper;
    /*@Autowired
    private CartDeleteRpc cartDeleteRpc;*/
    @Override public synchronized void addOrder(OrderAddParam param) {
        //查询订单orderSn是否存在订单 判断存在 和不存在 只有不存在方法才继续执行
        long count=orderMapper.countOrderByOrdersn(param.getOrderSn());
        if (count>0){
            //可以抛出业务异常
            return ;
        }
        // 减库存
        StockReduceCountParam stockReduceCountParam=new StockReduceCountParam();
        stockReduceCountParam.setReduceCount(param.getCount());
        stockReduceCountParam.setProductCode(param.getProductCode());
        stockReduceCountParam.setOrderSn(param.getOrderSn());
        stockApi.reduceStock(stockReduceCountParam);
        //stockService.reduceCommodityCount(stockReduceCountParam);
        //增订单
        OrderDO orderDO=new OrderDO();
        BeanUtils.copyProperties(param,orderDO);
        orderMapper.insertOrder(orderDO);
        //删除购物车
        Entry entry=null;
        try {
            entry= SphU.entry("delete-cart");
            //TODO 添加测试的thread.sleep
            CartDeleteParam cartDeleteParam = new CartDeleteParam();
            cartDeleteParam.setUserId(param.getUserId());
            cartDeleteParam.setProductCode(param.getProductCode());
            Boolean result = cartApi.deleteCart(cartDeleteParam);
            System.out.println("由于当前订单调用是异步调用所以result拿不到反馈:" + result);
            //如果想要通过异步调用 拿到调用反馈 需要额外编写代码
            RpcContext context = RpcContext.getContext();
            Future<Object> future = context.getFuture();
            //拿到异步反馈结果
            Object o = future.get();
            Boolean resultAsync = (Boolean) o;
            System.out.println("异步调用的回调函数获取的resultAsync拿到结果:" + resultAsync);
        }catch (BlockException e){
            //剪辑逻辑
            //记录日志 方便后续人工检查 哪张订单 哪个用户 哪个购物车商品没有删除
            System.out.println("记录当前订单信息");
        }catch (Exception e){
            System.out.println("记录RPC 或其它异常信息");
        } finally {
            if (entry!=null)entry.exit();
        }
    }
}
